/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao.gia;

import model.entity.Gia;
import model.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 *
 * @author luanp_000
 */
public class GiaDAO {

    public final static int insert(Gia gia) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        int id;
        try {

            session.beginTransaction();
            session.save(gia);
            session.getTransaction().commit();
            id = gia.getGiaMa();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw new HibernateException(e);
        } finally {
            session.close();
        }
        return id;
    }

    public static final void update(Gia gia) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.update(gia);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw new HibernateException(e);
        } finally {
            session.close();
        }
    }

    public static final void delete(Gia gia) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.delete(gia);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw new HibernateException(e);
        } finally {
            session.close();
        }
    }
}
